package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.StrategyDetailQueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService{
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper contentMapper;

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail) {
        if (strategyDetail.getState()==1) {
            strategyDetail.setReleaseTime(new Date());
        }
        StrategyContent content = strategyDetail.getContent();
        if (strategyDetail.getId() == null) {
            strategyDetail.setCreateTime(new Date());
            strategyDetailMapper.insert(strategyDetail);
            if (content!=null){
                content.setId(strategyDetail.getId());
                contentMapper.insert(content);
            }

        }else{
            if (content!=null){
                content.setId(strategyDetail.getId());
                contentMapper.updateByPrimaryKey(content);
            }
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);
        }
    }

    @Override
    public PageInfo query(StrategyDetailQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public StrategyDetail selectByDetailId(Long detailId) {
        return strategyDetailMapper.selectByPrimaryKey(detailId);
    }
}
